<template>
	<view>
		<u-checkbox-group
			v-model="val"
			:placement="placement"
			@change="checkboxChange"
			:disabled="disabled || !writable"
			iconPlacement="right"
			:borderBottom="true"
		>
			<u-checkbox
				v-for="(item, index) in checkboxData"
				:key="item[defaultProps.value]"
				:label="item[defaultProps.label] || item[defaultProps.value]"
				:name="item[defaultProps.value]"
				:customStyle="{ marginBottom: index == checkboxData.length - 1 ? 0 : '16rpx' }" 
			>
			</u-checkbox>
		</u-checkbox-group>
		<view v-show="required && validator" class="requiredMessage">{{ requiredMessage }}</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			val: [],
			validator: false
		};
	},
	props: {
		value: {
			type: Array,
			default: () => []
		},
		disabled: {
			type: Boolean,
			default: false
		},
		writable: {
			type: Boolean,
			default: true
		},
		placement: {
			type: String,
			default: 'column'
		},
		defaultProps: {
			type: Object,
			default: () => ({label: 'label', value: 'value'})
		},
		required: {
			type: Boolean,
			default: false
		},
		requiredMessage: {
			type: String,
			default: ''
		},
		checkboxData: {
			type: Array,
			default: () => ([])
		}
	},
	computed: {
		validateStatus() {
			return this.$store?.state?.validate?.status || false
		}
	},
	watch: {
		value: {
			handler () {
				this.val = this.value || []
			},
			immediate: true,
			deep: true
		},
		validateStatus() {
			if(this.validateStatus && this.required) {
				if(!this.val.length) {
					this.validator = true
				} else {
					this.validator = false
				}
			}
		},
		val() {
			if(this.validateStatus && this.required) {
				if(!this.val.length) {
					this.validator = true
				} else {
					this.validator = false
				}
			}
		}
	},
	methods: {
		// change
		checkboxChange(e) {
			this.$emit('input', e)
		}
	}
}
</script>

<style scoped>
.requiredMessage {
	width: 100%;
	height: 50rpx;
	line-height: 50rpx;
	color: #f56c6c;
	font-size: 22rpx;
}
</style>